package com.season.seasonStudy.javase.j_008_database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDao {
	static {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConn(){
		try {
			return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void closeAll(Connection conn,Statement ps,ResultSet rs)
	{
		try {
			if(rs!=null)
				rs.close();
			if(ps!=null)
				ps.close();
			if(conn!=null&&!conn.isClosed())
				conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	public static boolean executeUpdate(String sql,Object[] value){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn=BaseDao.getConn();
			ps=conn.prepareStatement(sql);
			for (int i = 0; value!=null && i < value.length; i++) {
				ps.setObject(i+1, value[i]);
			}
			int row=ps.executeUpdate();
			return row>0;
		}  
		catch (Exception e1) {
			e1.printStackTrace();
		}
		finally{
			BaseDao.closeAll(conn, ps, rs);
		}
		return false;
	}

}